<template>
  <div class="wrapper">
    <header>
      <i class="fa fa-angle-left" onclick="history.go(-1)"></i>
      <p>注册</p>
      <div></div>
    </header>
    <div class="top-ban"></div>
    <h1>欢迎注册</h1>
    <table>
      <tr>
        <td>手机号码</td>
        <td><input type="text" v-model="state.users.userId" placeholder="请输入手机号码"></td>
      </tr>
      <tr>
        <td>真实姓名</td>
        <td><input type="text" v-model="state.users.realName" placeholder="真实姓名便于查看体检报告"></td>
      </tr>
      <tr>
        <td>生日</td>
        <td><input type="date" v-model="state.users.birthday"></td>
      </tr>
      <tr>
        <td>性别</td>
        <td>
          <input type="radio" v-model="state.sex" value="1" checked>男
          <input type="radio" v-model="state.sex" value="0">女
        </td>
      </tr>
      <tr>
        <td>身份证号</td>
        <td><input type="text" v-model="state.users.identityCard" placeholder="请输入身份证号"></td>
      </tr>
      <tr>
        <td>密码</td>
        <td><input type="password" v-model="state.users.password" placeholder="请输入密码"></td>
      </tr>
      <tr>
        <td>确认密码</td>
        <td><input type="password" v-model="state.confirmPassword" placeholder="请再次输入密码"></td>
      </tr>
    </table>
    <div class="btn" @click="checkUsersById">完成</div>
  </div>
</template>

<script setup>
import { reactive } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';
import qs from 'qs';

const state = reactive({
  users: {
    userId: '',
    realName: '',
    birthday: '',
    sex: 1,
    identityCard: '',
    password: '',
  },
  confirmPassword: ''
});

const router = useRouter();

const checkUsersById = async () => {
  // 非空校验逻辑
  if (state.users.userId === '') {
    alert('用户的手机号不能为空');
    return;
  }
  if (state.users.realName === '') {
    alert('真实姓名不能为空');
    return;
  }
  if (state.users.birthday === '') {
    alert('生日不能为空');
    return;
  }
  if (state.users.identityCard === '') {
    alert('身份证号不能为空');
    return;
  }
  if (state.users.password === '') {
    alert('密码不能为空');
    return;
  }
  if (state.users.password !== state.confirmPassword) {
    alert('两次输入的密码不一致');
    return;
  }

  // 手机号唯一性校验请求
  try {
    const response = await axios.post(
      'UsersController/getUsersById',
      qs.stringify({ userId: state.users.userId })
    );

    // 返回 users 对象则表示存在
    const usersData = response.data;
    if (usersData && usersData.userId !== null) {
      alert('此用户的手机号已存在');
    } else {
      register();
    }
  } catch (error) {
    console.log(error);
  }
};

// 注册提交
const register = async () => {
  try {
    const response = await axios.post(
      'UserController/saveUsers',  // 接口路径
      qs.stringify(state.users)  // 请求参数
    );

    // 注册成功判断
    if (response.data > 0) {
      alert('注册成功');
      router.push('/login');
    } else {
      alert('注册失败');
    }
  } catch (error) {
    console.log(error);
  }
};
</script>


<style scoped>
/*********************** 总容器 ***********************/
.wrapper {
  width: 100%;
  height: 100%;
}

/*********************** header ***********************/
header {
  width: 100%;
  height: 15.7vw;
  background-color: #FFF;

  position: fixed;
  left: 0;
  top: 0;

  display: flex;
  align-items: center;
  justify-content: space-between;

  box-sizing: border-box;
  padding: 0 3.6vw;
}

header .fa {
  font-size: 8vw;
}

/*********************** 标题部分 ***********************/
h1 {
  font-size: 7.4vw;
  font-weight: 500;
  box-sizing: border-box;
  padding: 0 3.6vw;
  margin-top: 6vw;
}

/*********************** common样式 ***********************/
.top-ban {
  width: 100%;
  height: 15.7vw;
}

/*********************** table ***********************/
table {
  width: 92.8vw;
  margin: 0 auto;
  margin-top: 5vw;
  border-collapse: collapse;

  font-size: 4.2vw;
}

table tr td {
  height: 12vw;
  border-bottom: solid 1px #DDD;
}

table tr td input {
  border: none;
  outline: none;
}

/*********************** btn ***********************/
.btn {
  width: 92.8vw;
  margin: 0 auto;
  height: 12vw;
  margin-top: 8vw;
  background-color: #137E92;
  border-radius: 2vw;
  color: #FFF;
  font-size: 5vw;
  text-align: center;
  line-height: 12vw;

  user-select: none;
  cursor: pointer;
}
</style>